using MediatR;
using Microsoft.AspNetCore.Mvc;
using Movies.Application.Commands;
using Movies.Application.DTOs;
using Movies.Application.Queries;
using Movies.Application.Responses;



namespace Movies.API.Controllers;

public class MovieController : ApiController
{
    private readonly IMediator _mediator;
    private readonly ILogger<MovieController> _logger;

    public MovieController(IMediator mediator, ILogger<MovieController> logger)
    {
        _mediator = mediator;
        _logger = logger;
    }

    [HttpGet("all")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<ActionResult<IEnumerable<MovieResponse>>> GetMovies()
    {
        _logger.LogInformation("查询所有的movie");
        var query = new GetMoviesQuery();
        var result = await _mediator.Send(query);
        return Ok(result);
    }

    [HttpGet("movieName/{movieName}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<ActionResult<MovieResponse?>> GetMoviesByMovieName(string movieName)
    {
        var query = new GetMoviesByMovieNameQuery(movieName);
        GetMovieDto? result = await _mediator.Send(query);
        if (result == null)
        {
            return NoContent();
        }
        return Ok(result);
    }

    [HttpGet("directorName/{directorName}")]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<ActionResult<IEnumerable<MovieResponse>>> GetMoviesByDirectorName(
        string directorName
    )
    {
        var query = new GetMoviesByDirectorNameQuery(directorName);
        var result = await _mediator.Send(query);
        return Ok(result);
    }

    [HttpPost]
    [ProducesResponseType(StatusCodes.Status200OK)]
    public async Task<ActionResult<MovieResponse>> CreateMovie(
        [FromBody] CreateMovieCommand command
    )
    {
        var result = await _mediator.Send(command);
        return Ok(result);
    }
}
